package com.example.as_sum.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.as_sum.R;
import com.example.as_sum.adapter.RecyclerViewAdapter;
import com.example.as_sum.entity.ShopInfo;
import com.example.as_sum.view.SpaceItemDecoration;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class RecyclerViewActivity extends RootActivity {

    @Bind(R.id.recyclerview)
    XRecyclerView recyclerview;
    RecyclerViewAdapter adapter;
    List<ShopInfo> shops;
    @Bind(R.id.img_back)
    ImageView mImgBack;
    @Bind(R.id.tv_cancle)
    TextView mTvCancle;
    @Bind(R.id.tv_empty)
    TextView mEmpty;
    @Bind(R.id.layout_select)
    LinearLayout mLayoutSelect;

    String [] imgList=new String[]{
            "http://p3.so.qhmsg.com/sdr/512_768_/t012e7c2c11b1a6300d.jpg",
            "http://p1.so.qhmsg.com/sdr/1152_768_/t01abda585cbf43c1b6.jpg",
            "http://p2.so.qhmsg.com/sdr/512_768_/t016615986f4dff7302.jpg",
            "http://p0.so.qhmsg.com/sdr/512_768_/t01f2a38dfa25d56725.jpg",
            "http://p0.so.qhmsg.com/sdr/1128_768_/t014ee8509d34bb9410.jpg",
            "http://p1.so.qhmsg.com/sdr/1071_768_/t01b2c6cb9ffd94963b.jpg",
            "http://p1.so.qhmsg.com/sdr/1152_768_/t016e2e1a69ed42b561.jpg",
            "http://p1.so.qhmsg.com/sdr/1170_768_/t01b58d41e90bc51cb6.jpg",
            "http://p4.so.qhmsg.com/sdr/1855_768_/t01b4a3cc310de530de.jpg",
            "http://p4.so.qhmsg.com/sdr/1168_768_/t015e031ab8afb87754.jpg",
            "http://p0.so.qhmsg.com/sdr/1152_768_/t011eb2a9af41c6dd0a.jpg",
            "http://p3.so.qhmsg.com/sdr/1061_768_/t017ec838c2bf0a02ad.jpg",
            "http://p3.so.qhmsg.com/sdr/1152_768_/t01167781143db59c78.jpg",
            "http://p4.so.qhmsg.com/sdr/1155_768_/t0173a8d2ec3818073e.jpg",
            "http://p0.so.qhmsg.com/t01e1b560ee5da65aff.jpg",
            "http://p4.so.qhmsg.com/sdr/1209_768_/t01933a30d37fb4009e.jpg",
            "http://p2.so.qhmsg.com/sdr/1024_768_/t01c58668b484365c1c.jpg",
            "http://p0.so.qhmsg.com/t01f12c7fe41ca8202f.jpg",
            "http://p4.so.qhmsg.com/sdr/1024_768_/t014f56eb8fd23035c9.jpg",
            "http://p0.so.qhmsg.com/sdr/1126_768_/t01d4a89b0b664a261a.jpg",
            "http://p1.so.qhmsg.com/t01f72e5b29642a3d26.jpg",
            "http://p4.so.qhmsg.com/sdr/1023_768_/t0173b4f5e4e93afa19.jpg",
            "http://p4.so.qhmsg.com/t015d63edf3ac285279.jpg",
            "http://p4.so.qhmsg.com/sdr/762_768_/t0169e3f2fa732894a9.jpg",
            "http://p2.so.qhmsg.com/t0114a10409bede7417.jpg",
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycler_view);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        shops = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            ShopInfo shop = new ShopInfo();
            shop.setSname("商品10" + i);
            shop.setImg(imgList[i]);
            shops.add(shop);
        }
        adapter = new RecyclerViewAdapter(this, shops);
        recyclerview.setLayoutManager(new LinearLayoutManager(this));
        recyclerview.addItemDecoration(new SpaceItemDecoration(20));
        recyclerview.setAdapter(adapter);
        recyclerview.setEmptyView(mEmpty);
        recyclerview.setPullRefreshEnabled(true);
        recyclerview.setPullMoreEnabled(true);
        recyclerview.setRefreshProgressStyle(ProgressStyle.SysProgress);
//        recyclerview.setEmptyView();
//        recyclerview.addFootView();
//        recyclerview.addHeaderView();
//        recyclerview.setArrowImageView(R.drawable.sc);//设置刷新的图标
//        recyclerview.setRefreshHeader();//设置刷新布局
        recyclerview.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                shops.clear();
                mHandler.sendEmptyMessageDelayed(0, 2000);
            }

            @Override
            public void onLoadMore() {
                mHandler.sendEmptyMessageDelayed(1, 2000);
            }
        });
    }

    private Handler mHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    List<ShopInfo> mList=new ArrayList<>();
                    for (int i = 0; i < 8; i++) {
                        ShopInfo shop = new ShopInfo();
                        shop.setSname("商品10" + i);
                        shop.setImg(imgList[i]);
                        mList.add(shop);
                    }
                    shops=mList;
                    adapter.updateData(shops);
                    adapter.notifyDataSetChanged();
                    recyclerview.refreshComplete();

                    if (shops.size() > 0) {
                        mLayoutSelect.setVisibility(View.VISIBLE);
                    }else{
                        mLayoutSelect.setVisibility(View.GONE);
                    }
                    break;
                case 1:
                    if (shops.size() <= 31) {
                        for (int i = 0; i < 14; i++) {
                            ShopInfo shop = new ShopInfo();
                            shop.setSname("新增：商品100" + i);
                            shop.setImg(imgList[i]);
                            shops.add(shop);
                        }
                        adapter.updateData(shops);
                        recyclerview.loadMoreComplete();
                    } else {
                        recyclerview.setNoMore(true);
                    }
                    break;
            }
            return false;
        }
    });

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }
    boolean isAllShow;
    @OnClick({R.id.img_back, R.id.tv_cancle, R.id.tv_select, R.id.tv_delete})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.tv_cancle:
                toggleStatus(0);
                adapter.setAllShow(false);
                break;
            case R.id.tv_select:
                if(!isAllShow){
                    adapter.setAllShow(true);
                    toggleStatus(1);
                }else{
                    setSelect(true);
                }

                break;
            case R.id.tv_delete:
                getSelectId();
                break;
        }
    }

    private void setSelect(boolean is) {
        for (ShopInfo info : shops) {
            info.setState(is);
        }
        adapter.updateData(shops);
    }
    private void getSelectId(){
        List<String> ids=new ArrayList<>();
        for (ShopInfo info:shops){
            boolean status=info.isState();
            if(status){
                ids.add(info.getSid()+"");
            }
        }
        Toast.makeText(this,"id："+ids.toString(),Toast.LENGTH_LONG).show();
    }
    private void toggleStatus(int index) {
        if (index == 0) {
            mImgBack.setVisibility(View.VISIBLE);
            mTvCancle.setVisibility(View.GONE);
        } else if (index == 1) {
            mImgBack.setVisibility(View.GONE);
            mTvCancle.setVisibility(View.VISIBLE);
        }
    }
}
